//controlls
define(function (require, exports, module) {
    const Vue=require("vue");
    var template=require('text!./index.html');
    const api=require("api");
    const sdk=require("sdk");
    const weui=require('weui');
    const utils=require('utils');
    module.exports=Vue.extend({
        template:template,
        data:function () {
            return {

            }
        },
        computed:{
            oauth:function () {
                return this.$store.getters.getOAuth;
            },
            sdkInit:function () {
                return this.$store.getters.getSdkInit;
            },
            isBack:function () {
                return this.$store.getters.getIsBack;
            },
            user_info:function () {
                return this.$store.getters.getUserInfo;
            }
        },
        beforeRouteEnter:function (to,from,next) {
            next(function (vm) {
                if(!vm.isBack) {
                    if (vm.user_info.user_certification == 0) {//实名认证判断
                        vm.confirm('您还未完成实名认证，是否去认证？', function () {
                            vm.$router.push({path: '/name'});
                        });
                    } else if (vm.user_info.deposit == 0) {//已交押金判断
                        vm.confirm('您还未缴纳押金，是否去缴纳？', function () {
                            vm.$router.push({path: '/deposit'});
                        });
                    }
                    if (!vm.isBack) {
                        //判断用户真在用车
                        if (vm.user_info.use_bikecode && vm.user_info.use_starttime) {
                            //跳转真在骑行页面
                            vm.$router.push({path: '/cycling/' + vm.user_info.use_bikecode + '/' + vm.user_info.use_starttime});
                        }
                    }
                }
            })
        },
        methods:{
            confirm:function (title,callback) {
                weui.confirm(title, {
                    title: '',
                    buttons: [{
                        label: '取消',
                        type: 'default',
                        onClick: function(){  }
                    }, {
                        label: '确定',
                        type: 'primary',
                        onClick: function(){ callback();}
                    }]
                });
            },
            rechargeConfirm:function () {
                var v=this;
                weui.confirm('余额不足，是否前往充值？', {
                         title: '',
                         buttons: [{
                                label: '取消',
                                type: 'default',
                                onClick: function(){  }
                            }, {
                                 label: '去充值',
                                 type: 'primary',
                                 onClick: function(){  v.$router.push({path:'/recharge'})}
                            }]
                 });
            },
            scanQrcode:function () {
                var v=this;
                if(!v.oauth.open_id||v.oauth.open_id==="undefined"){
                    window.location.href=utils.oauthUrl();
                    return;
                }
                if (v.user_info.user_certification == 0) {//实名认证判断
                    v.confirm('您还未完成实名认证，是否去认证？',function () {
                        v.$router.push({path: '/name'});
                    });
                    return;
                } else if (v.user_info.deposit == 0) {//已交押金判断
                    v.confirm('您还未缴纳押金，是否去缴纳？',function () {
                        v.$router.push({path: '/deposit'});
                    });
                    return;
                }
                if(parseInt(v.user_info.recharge)<0){
                    v.rechargeConfirm();
                    return;
                }
                if(v.sdkInit){
                    sdk.scan().then(function (res) {
                        if(/:ok$/i.test(res.errMsg)){
                            var code=utils.getQrcode(res.resultStr);
                            var loading = weui.loading('正在开锁……');
                            api.useBike(0,code,v.oauth.open_id,v.user_info).then(function (res) {
                                var data=res.user_use_bike;
                                if(data&&data.return_code==="SUCCESS"){
                                    //更新用户数据
                                    v.$store.dispatch('UserInfo',v.user_info);
                                    weui.toast("开锁成功！",{callback:function () {
                                        //跳转真在骑行页面
                                        v.$router.push({path:'/cycling/'+data.bike_code+'/'+data.unlock_time});
                                    }});
                                }else {
                                    weui.topTips(data.return_msg);
                                }
                            }).catch(function (e) {
                                weui.topTips("网络异常，请刷新页面再试!");
                            }).finally(function () {
                                loading.hide();
                            });
                        }
                    }).catch(function (e) {
                        weui.topTips('扫码调用失败')
                    })
                }else{
                    this.$store.dispatch('SdkInit');
                }
            }
        }
    });
});
